from datetime import datetime
from typing import Optional, Dict, Annotated

import orjson
import pytz
from pydantic import BaseModel

from fastapi import Depends
from src.core.pipeline import BasicPipeline, PipelineManager


class MetadataTraceable:
    def with_metadata(self) -> dict:
        return {
            "resource": self,
            "metadata": {
                **self._error_metadata(),
            },
        }

    def _error_metadata(self):
        return {
            "error_type": self.error and self.error.code,
            "error_message": self.error and self.error.message,
        }


class Configuration(BaseModel):
    class FiscalYear(BaseModel):
        start: str
        end: str

    class Timezone(BaseModel):
        name: str = "UTC"
        utc_offset: str = ""  # Deprecated, will be removed in the future

    def show_current_time(self):
        # Get the current time in the specified timezone
        tz = pytz.timezone(
            self.timezone.name
        )  # Assuming timezone.name contains the timezone string
        current_time = datetime.now(tz)

        return f"{current_time.strftime('%Y-%m-%d %A %H:%M:%S')}"  # YYYY-MM-DD weekday_name HH:MM:SS, ex: 2024-10-23 Wednesday 12:00:00

    fiscal_year: Optional[FiscalYear] = None
    language: Optional[str] = "English"
    timezone: Optional[Timezone] = Timezone()


class SSEEvent(BaseModel):
    class SSEEventMessage(BaseModel):
        message: str

        def to_dict(self):
            return {"message": self.message}

    data: SSEEventMessage

    def serialize(self):
        return f"data: {orjson.dumps(self.data.to_dict()).decode()}\n\n"


# 修改 PipelineManager 的依赖注入方式
def get_pipeline_manager(pipelines: Dict[str, BasicPipeline]) -> PipelineManager:
    """Get a pipeline manager instance."""
    return PipelineManager(pipelines)

# 使用 Annotated 类型提示来避免 FastAPI 尝试将 PipelineManager 作为响应模型
PipelineManagerDep = Annotated[PipelineManager, Depends(get_pipeline_manager)]

# 导出所有服务
__all__ = [
    "Configuration",
    "MetadataTraceable",
    "PipelineManagerDep",
    "SSEEvent",
]

# 注意：以下导入必须放在最后，以避免循环导入
# 这些导入仅用于让其他模块能够直接从 src.web.v1.services 导入这些服务
# 不要在上面的代码中使用这些导入的服务
from .ask import AskService  # noqa: E402
from .ask_details import AskDetailsService  # noqa: E402
from .chart import ChartService  # noqa: E402
from .chart_adjustment import ChartAdjustmentService  # noqa: E402
from .question_recommendation import QuestionRecommendation  # noqa: E402
from .relationship_recommendation import RelationshipRecommendation  # noqa: E402
from .semantics_description import SemanticsDescription  # noqa: E402
from .semantics_preparation import SemanticsPreparationService  # noqa: E402
from .sql_answer import SqlAnswerService  # noqa: E402
from .sql_expansion import SqlExpansionService  # noqa: E402
from .sql_pairs import SqlPairsService  # noqa: E402
from .sql_question import SqlQuestionService  # noqa: E402

# 更新 __all__ 以包含所有服务
__all__ += [
    "AskService",
    "AskDetailsService",
    "ChartService",
    "ChartAdjustmentService",
    "QuestionRecommendation",
    "RelationshipRecommendation",
    "SemanticsDescription",
    "SemanticsPreparationService",
    "SqlAnswerService",
    "SqlExpansionService",
    "SqlPairsService",
    "SqlQuestionService",
]
